package com.ceo.common.i18.domain;

import org.apache.logging.log4j.util.Strings;

import java.util.Locale;

/**
 * @author uto_dev_008
 * @desc
 * @since 2021/11/22 16:53
 * Copyright (c)  All rights reserved
 */
public enum LanguageTypeEnum {
    //简体中文
    LANGUAGE_ZH_CN("zh_CN"),
    //美式英文
    LANGUAGE_EN_US("en_US"),
    ;
    private String languageType;

    /*
        默认语种 中文
     */
    private static final LanguageTypeEnum DeafaultlanguageType = LANGUAGE_ZH_CN;

    LanguageTypeEnum(String languageType) {
        this.languageType = languageType;
    }

    public String getLanguageType() {
        return languageType;
    }

    /**
     * 获取指定语言类型(如果没有对应的语言类型,则返回中文)
     *
     * @param language 语言类型
     * @return 返回系统支持的语言类型
     */
    public static String getLanguageType(String language) {
        if (Strings.isBlank(language)) {
            return DeafaultlanguageType.languageType;
        }
        for (LanguageTypeEnum languageEnum : LanguageTypeEnum.values()) {
            if (languageEnum.languageType.equalsIgnoreCase(language)) {
                return languageEnum.languageType;
            }
        }
        switch (language.toLowerCase()) {
            case "en":
                return LanguageTypeEnum.LANGUAGE_EN_US.languageType;
            case "zh":
                return LanguageTypeEnum.LANGUAGE_ZH_CN.languageType;
        }

        return DeafaultlanguageType.languageType;
    }

    private static Locale getLanguageType(LanguageTypeEnum language) {
        if (language == LanguageTypeEnum.LANGUAGE_EN_US) {
            return Locale.US;
        }
        return Locale.CHINA;
    }

    /**
     * 获取指定语言类型(如果没有对应的语言类型,则返回中文)
     *
     * @param language 语言类型
     * @return 返回语言对应的区域
     */
    public static Locale getLanguageLocale(String language) {
        if (Strings.isBlank(language)) {
            return getLanguageType(DeafaultlanguageType);
        }
        for (LanguageTypeEnum languageEnum : LanguageTypeEnum.values()) {
            if (languageEnum.languageType.equalsIgnoreCase(language)) {
                return getLanguageType(languageEnum);
            }
        }
        return getLanguageType(DeafaultlanguageType);
    }
}
